package com.cskaoyan.market.service.impl;

import com.cskaoyan.market.db.domain.MarketAd;
import com.cskaoyan.market.db.domain.MarketAdExample;
import com.cskaoyan.market.db.domain.MarketIssueExample;
import com.cskaoyan.market.db.mapper.MarketAdMapper;
import com.cskaoyan.market.service.AdminAdService;
import com.cskaoyan.market.util.MybatisUtils;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class AdminAdServiceImpl implements AdminAdService {


    @Override
    public int delete(MarketAd marketAd) {

        SqlSession session = MybatisUtils.getSession();

        int i = session.getMapper(MarketAdMapper.class).deleteByPrimaryKey(marketAd.getId());

        session.commit();
        session.close();
        return i;

    }

    @Override
    public int update(MarketAd marketAd) {

        SqlSession session = MybatisUtils.getSession();

        int i = session.getMapper(MarketAdMapper.class).updateByPrimaryKey(marketAd);

        session.commit();
        session.close();
        return i;

    }

    @Override
    public int create(MarketAd marketAd) {
        SqlSession session = MybatisUtils.getSession();
        MarketAdMapper adMapper = session.getMapper(MarketAdMapper.class);
        MarketIssueExample marketIssueExample = new MarketIssueExample();

        int i = adMapper.insertSelective(marketAd);

        session.commit();
        session.close();
        return i;

    }

    @Override
    public List<MarketAd> insert(String id, String name, String link, String url, String position, String content, String position1, String endTime, String updateTime, String page, String limit) {
//list的
        SqlSession session = MybatisUtils.getSession();
        MarketAdMapper adMapper = session.getMapper(MarketAdMapper.class);
        MarketAdExample marketAdExample = new MarketAdExample();
        MarketAdExample.Criteria criteria = marketAdExample.createCriteria();

        //根据广告标题和内容进行模糊查询
        if (name != null && content == null) {
            criteria.andNameLike("%" + name + "%");
        }
        if (content != null && name == null) {
            criteria.andContentLike("%" + content + "%");
        }
        if (content != null && name != null) {
            criteria.andContentLike("%" + content + "%");
            criteria.andNameLike("%" + name + "%");
        }

        PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(limit));

        List<MarketAd> marketAdList = adMapper.selectByExample(marketAdExample);
        session.close();
        return marketAdList;




    }

}
